package cn.nbt.busManage.controller;


import cn.nbt.busManage.common.ResponseResult;
import cn.nbt.busManage.model.dto.NewsDto;
import cn.nbt.busManage.model.vo.NewsVo;
import cn.nbt.busManage.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;


    /**
     * 查询所有新闻
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<Object> findAll() {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<NewsDto> all = newsService.findAll();
            if (all == null){
                result.Error();
                return result;
            }
            ArrayList<NewsVo> newsVos = new ArrayList<>();
            for (NewsDto newsDto : all){
                newsVos.add(new NewsVo(newsDto));
            }
            result.Success("查询成功",all);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error("查询失败");
        }
        return result;
    }

    /**
     * 根据作者查询新闻
     * @param author
     * @return
     */
    @GetMapping("/findByQuery")
    public ResponseResult<Object>findByQuery(@RequestParam(name="author") String author){
        ResponseResult <Object> result=new ResponseResult<>();
        try {
            List<NewsDto> newsDtolist=newsService.findByQuery(author);
            if (newsDtolist == null){
                result.BadRequest();
                return result;
            }
            ArrayList<NewsVo> newsVos = new ArrayList<>();
            for (NewsDto newsDto :newsDtolist) {
                newsVos.add(new NewsVo(newsDto));
            }
            result.Success("查询成功",newsVos);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error("查询失败");
        }
        return result;
    }

    /**
     * 删除新闻信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ResponseResult<Object> delete(@RequestParam(name = "id") Integer id){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            newsService.delete(id);
            result.Success("删除成功");
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error("删除失败");
        }
        return result;
    }

    /**
     * 增加新闻信息
     * @param newsVo
     * @return
     */
    @PostMapping("/add")
    public ResponseResult<Object> add(@RequestBody NewsVo newsVo){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            NewsDto newsDto = new NewsDto(newsVo);
            newsService.add(newsDto);
            result.Success("添加成功");
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error("添加失败");
        }
        return result;
    }

    /**
     * 更新新闻信息
     * @param newsVo
     * @return
     */
    @PutMapping("/update")
    public ResponseResult<Object> update(@RequestBody NewsVo newsVo){
        ResponseResult<Object>result = new ResponseResult<>();
        try{
            NewsDto newsDto = new NewsDto(newsVo);
            newsService.update(newsDto);
            result.Success("更新成功");
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error("更新失败");
        }
        return result;
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public ResponseResult<Object> findById (@RequestParam(name="id") Integer id){
        ResponseResult <Object> result=new ResponseResult<>();
        try {
            NewsDto dto = newsService.findById(id);
            if (dto == null){
                result.BadRequest();
                return result;
            }
            NewsVo newsVo = new NewsVo(dto);
            result.Success("查询成功",newsVo);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error("查询失败");
        }
        return result;
    }

}
